import { getAppIDInfo, getAppName } from "@/config/template";
import userState from "@/models/userState";
import configWifi from "@/pages/configDevice/models/configWifi";
import Taro from "@tarojs/taro";
import { version as gizwitsSdkVersion } from "mini_program_gizwits_sdk/package.json";
import { toJS } from "mobx";

interface MobileInfo {
  brand: string;
  model: string; // 设备型号。新机型刚推出一段时间会显示unknown，微信会尽快进行适配。
  pixelRatio: number;
  systemVersion: string; // 操作系统版本
  platform: string; // 客户端平台
}

interface AppInfo {
  wxSdkVersion: string; // 客户端基础库版本
  gizwitsSdkVersion: string; // Gizwits SDK 版本
  version: string; // 微信版本号
  gizwitsAppID: string; // gizwitsAppID
  wxAppID: string; // 微信小程序AppID
  applicationName: string; //小程序名称
  bluetoothEnabled?: boolean; // 蓝牙是否可用
  locationEnabled?: boolean; // 定位是否可用
  wifiEnabled?: boolean; // wifi是否可用
}

class Monitor {
  mobileInfo: MobileInfo;
  appInfo: AppInfo;
  constructor() {
    Taro.getSystemInfoAsync({
      success: (res) => {
        const appInfo: AppInfo = {
          wxSdkVersion: res.SDKVersion,
          gizwitsSdkVersion,
          appID: getAppIDInfo().appId,
          applicationName: getAppName("zh"),
          bluetoothEnabled: res.bluetoothEnabled,
          locationEnabled: res.locationEnabled,
          wifiEnabled: res.wifiEnabled,
        };

        const mobileInfo: MobileInfo = {
          brand: res.brand,
          model: res.model,
          pixelRatio: res.pixelRatio,
          systemVersion: res.system,
          platform: res.platform,
        };

        this.mobileInfo = mobileInfo;
        this.appInfo = appInfo;
      },
    });
  }
}

const monitor = new Monitor();

export function getStartReportData(monitorTarget: any) {
  const curDate = new Date();
  const eventID = createUUID32();
  const mobileInfo = monitor.mobileInfo;
  const appInfo = monitor.appInfo;
  configWifi.eventID = eventID;

  return {
    origin:"MINI-WX",
    mobileInfo,
    userInfo: toJS(userState.userInfo),
    appInfo,
    eventID,
    eventType: "ConfigNetWorkInfoStart",
    monitorTarget,
    time: curDate,
    timeStamp: Math.floor(curDate.getTime() / 1000),
  };
}

export function getEndReportData(
  devices: IDevice[],
  paasRes: any,
  saasRes: any,
  sdkRes:any
) {
  const mobileInfo = monitor.mobileInfo;
  const appInfo = monitor.appInfo;
  const curDate = new Date();

  return {
    origin:"MINI-WX",
    mobileInfo,
    userInfo: toJS(userState.userInfo),
    appInfo,
    eventType: "ConfigNetWorkInfoEnd",
    eventID: configWifi.eventID,
    time: curDate,
    timeStamp: Math.floor(curDate.getTime() / 1000),
    monitorTarget: {
      devices,
      sdkResult: sdkRes,
      paasResult: paasRes,
      saasResult: saasRes,
    },
  };
}

function createUUID32() {
  let dt = new Date().getTime();
  let uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
    /[xy]/g,
    function (c) {
      let r = (dt + Math.random() * 16) % 16 | 0;
      dt = Math.floor(dt / 16);
      return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
    }
  );
  return uuid;
}
